import log4js from 'log4js'
import fs from 'fs'
import { isDirectory } from '../common/utils/utils'

//检查某个目录是否存在

if (!isDirectory('logs')) {
  fs.mkdirSync('logs')
}
if (!isDirectory('logs/date')) {
  fs.mkdirSync('logs/date')
}

log4js.configure({
  appenders: {
    console: {
      type: 'console',
    },
    file: {
      type: 'file',
      filename: 'logs/file.log',
      category: 'normal',
      maxLogSize: 104800,
      backups: 100,
    },
    date: {
      type: 'dateFile',
      filename: 'logs/date/',
      category: 'normal',
      alwaysIncludePattern: true,
      pattern: '-yyyy-MM-dd-hh.log',
    },
  },
  categories: {
    default: {
      appenders: ['console', 'file', 'date'],
      level: 'info',
    },
  },
})

const logger = log4js.getLogger('cheese')

export default logger
